package org.sport.logic;

import org.sport.dbInterface.AwardOperation;
import org.sport.dbInterface.UserInfoHisOperation;
import org.sport.dbInterface.UserOperation;
import org.sport.dbInterface.UserPrivateOperation;
import org.sport.init.SportClientSymbol;
import org.sport.init.SportServerSymbol;
import org.sport.model.entityModel.AwardtableEntity;
import org.sport.model.entityModel.UserinfohistorytableEntity;
import org.sport.model.entityModel.UserprivatetableEntity;
import org.sport.model.entityModel.UsertableEntity;
import org.sport.model.jsonModel.JsonImageString;
import org.sport.model.jsonModel.Json_5_detail;
import org.sport.model.jsonModel.Json_5_sign;
import org.sport.model.jsonModel.Json_5_updateDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;

/**
 * Created by zehong
 *
 * 关于页面“我”的逻辑处理，包括获取个人信息，签到等
 */

@Service
public class Logic_AboutMe {

    @Autowired
    UserOperation userOperation;

    @Autowired
    AwardOperation awardOperation;

    @Autowired
    LogicUtils logicUtils;

    @Autowired
    UserPrivateOperation userPrivateOperation;

    @Autowired
    UserInfoHisOperation userInfoHisOperation;


    //获取我的信息
    public Json_5_detail detail(Json_5_detail json_5_detail)
    {
        System.out.println("进入Logic-5_detail");

        //找到用户表
        UsertableEntity user=logicUtils.findUser(json_5_detail.getPhoneNumber());
        //找到用户隐私表
        UserprivatetableEntity userprivate=userPrivateOperation.findUserPriByUserId(user);
        //找到该用户的签到表
        AwardtableEntity award=logicUtils.findAwardByUser(user);

        if (user==null){
            //没有该用户
            System.out.println("获取错误，没有该用户");
            json_5_detail.setResult(SportClientSymbol.Me.DETAIL_RESULT_FAIL);
        }else {
            System.out.println("找到该用户");
            if (userprivate==null){
                System.out.println("获取错误，没有该用户的隐私表");
                json_5_detail.setResult(SportClientSymbol.Me.DETAIL_RESULT_FAIL);
            }else {
                if (award==null){
                    System.out.println("获取错误，没有该用户的签到表");
                    json_5_detail.setResult(SportClientSymbol.Me.DETAIL_RESULT_FAIL);
                }else {
                    //填充返回数据
                    if (user.getHeadIcon()!=null){
                        json_5_detail.setIcon(user.getHeadIcon());
                    }
                    if (user.getName()!=null){
                        json_5_detail.setNickname(user.getName());
                    }
                    if (award.getTot()!=null){
                        json_5_detail.setContinueSignDay(award.getTot());
                    }
                    if (userprivate.getSex()!=null){
                        json_5_detail.setSex(userprivate.getSex());
                    }
                    if (user.getBirthday()!=null){
                        json_5_detail.setBirthday(user.getBirthday().toString());
                    }
                    if (userprivate.getWeight()!=null){
                        json_5_detail.setWeight( Float.parseFloat(String.valueOf(userprivate.getWeight())));
                    }
                    if (userprivate.getHeight()!=null){
                        json_5_detail.setHeight(userprivate.getHeight());
                    }
                    if (userprivate.getUserName()!=null){
                        json_5_detail.setWalkNumGoal(Integer.parseInt(userprivate.getUserName()));
                    }
                    json_5_detail.setResult(SportClientSymbol.Me.DETAIL_RESULT_SUCCESS);
                }
            }
        }

        return json_5_detail;
    }


    //更新我的信息
    public Json_5_updateDetail updateDetail(Json_5_updateDetail json_5_updateDetail){

        System.out.println("进入Logic-5_updateDetail");

        //找到用户表
        UsertableEntity user=logicUtils.findUser(json_5_updateDetail.getPhoneNumber());
        //找到用户隐私表
        UserprivatetableEntity userprivate=userPrivateOperation.findUserPriByUserId(user);


        if (user==null){
            System.out.println("没有该用户表");
            json_5_updateDetail.setResult(SportClientSymbol.Me.UPDATEDETAIL_RESULT_FAIL);
        }else {
            if (userprivate==null){
                System.out.println("没有该用户隐私表");
                json_5_updateDetail.setResult(SportClientSymbol.Me.UPDATEDETAIL_RESULT_FAIL);
            }else {
                //更新user表
                //给用户头像起个名字
                String iconName=json_5_updateDetail.getPhoneNumber()+"_"+new Date().getTime()+".jpg";
                //获取用户头像，并保存到服务器图片地址中
                boolean getImageResult=false;
                boolean getImageResult2=false;
                try{
                    //getImageResult= JsonImageString.generateImage(json_5_updateDetail.getIconString(), SportServerSymbol.tomcatImgPath+"\\"+iconName);
                    //getImageResult2= JsonImageString.generateImage(json_5_updateDetail.getIconString(), SportServerSymbol.ImgStoragePath+"\\"+iconName);
                    getImageResult= JsonImageString.generateImage(json_5_updateDetail.getIconString(), SportServerSymbol.tomcatImgPath+"/"+iconName);
                    getImageResult2= JsonImageString.generateImage(json_5_updateDetail.getIconString(), SportServerSymbol.ImgStoragePath+"/"+iconName);
                }catch (Exception e){
                    e.printStackTrace();
                }
                if(getImageResult&&getImageResult2){
                    //获取图片成功
                    String iconPath="/0/upload_img/"+iconName;
                    System.out.println("图片在服务器地址为"+iconPath);

                    json_5_updateDetail.setIconURL(iconPath);
                    user.setHeadIcon(iconPath);
                    user.setName(json_5_updateDetail.getNickname());
                    user.setBirthday(Timestamp.valueOf(json_5_updateDetail.getBirthday()));
                    boolean updateUserResult=userOperation.updateUserEntity(user);

                    //更新userprivate表
                    userprivate.setSex(json_5_updateDetail.getSex());
                    userprivate.setWeight(Double.parseDouble(String.valueOf(json_5_updateDetail.getWeight())));
                    userprivate.setHeight(json_5_updateDetail.getHeight());
                    userprivate.setUserName(json_5_updateDetail.getWalkNumGoal()+"");
                    boolean updateUserPrivateResult=userPrivateOperation.updateUserPri(userprivate);

                    //同时加一条身高体重的历史信息到个人历史情况表中
                    UserinfohistorytableEntity userinfohistory=new UserinfohistorytableEntity();
                    userinfohistory.setTime(new Timestamp(System.currentTimeMillis()));
                    userinfohistory.setWeight(Double.parseDouble(String.valueOf(json_5_updateDetail.getWeight())));
                    userinfohistory.setOtherData(json_5_updateDetail.getHeight()+"");
                    userinfohistory.setUsertableByUserId(user);
                    boolean addUserInfoHisResult=userInfoHisOperation.addUserInfoHisOne(userinfohistory);


                    if (updateUserResult&&updateUserPrivateResult&&addUserInfoHisResult){
                        System.out.println("更新用户信息成功");
                        json_5_updateDetail.setResult(SportClientSymbol.Me.UPDATEDETAIL_RESULT_SUCCESS);
                    }else {
                        System.out.println("更新用户信息失败");
                        json_5_updateDetail.setResult(SportClientSymbol.Me.UPDATEDETAIL_RESULT_FAIL);
                    }

                }else{
                    //获取用户头像失败
                    System.out.println("获取用户头像失败");
                    json_5_updateDetail.setResult(SportClientSymbol.Me.UPDATEDETAIL_RESULT_FAIL_IMAGEERROR);
                }
            }
        }

        return json_5_updateDetail;
    }



    //签到
    public Json_5_sign sign(Json_5_sign json_5_sign){

        System.out.println("进入Logic-5_sign");

        //找到用户
        UsertableEntity user=logicUtils.findUser(json_5_sign.getPhoneNumber());
        //找到该用户的签到表
        AwardtableEntity award=logicUtils.findAwardByUser(user);
        boolean signResult=awardOperation.totalAwardAddOne(award);

        if (signResult){
            //签到成功
            System.out.println("签到成功");
            json_5_sign.setResult(SportClientSymbol.Me.SIGN_RESULT_SUCCESS);
        }else {
            //签到失败
            System.out.println("签到失败");
            json_5_sign.setResult(SportClientSymbol.Me.SIGN_RESULT_FAIL);
        }

        return json_5_sign;
    }
}
